﻿﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace iHub
{
    public partial class Form2 : Form
    {

        AssignmentManager assm = new AssignmentManager();
        public Form2(Assignment n)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Start_Closing);
            RequestingUnit.Text = n.Request;
            Address.Text = n.Address;
            City.Text = n.City;
            BatchNo.Text = n.Batch;
            Desc.Text = n.Assign;
            Assign.Text = n.AssignId;
            startDate.Text = n.Startdate;
            endDate.Text = n.Enddate;
            List<string> a = new List<string>();
            a = assm.getRates();


            if (n.Status == "Pending")
            {
                Status.Text = n.Status;
                Status.Items.Add("Pending");
                Status.Items.Add("Complete");
                Status.Items.Add("New");

            }

            if (n.Status == "Complete")
            {
                Status.Text = n.Status;
                Status.Items.Add("Pending");
                Status.Items.Add("Complete");
                Status.Items.Add("New");

            }

            if (n.Status == "New")
            {
                Status.Text = n.Status;
                Status.Items.Add("Pending");
                Status.Items.Add("Complete");
                Status.Items.Add("New");

            }
            String dash = "-";

            Type1.Text = n.Type;

            for (int i = 0; i < a.Count; i++)
            {
                Type1.Items.Add(a[i]);
            }


            Edit.DataSource = assm.EmployeeAssign();
            if (n.Ename != "")
            {
                Edit.SelectedItem = n.Ename += dash += n.Eid;
            }
            else
            {
                Edit.SelectedIndex = -1;
            }

        }

        private void Form2_Load(object sender, EventArgs e)
        {


        }

        private void Start_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                main.form.Enabled = true;
                main.form.assEditDisplay_Refresh();
            }
        }

        private void assAddConf_Click(object sender, EventArgs e)
        {
            if (RequestingUnit.Text == "" || Address.Text == "" || City.Text == "" || BatchNo.Text == "" || Desc.Text == "" || Assign.Text == "" || startDate.Text == "" || endDate.Text == "" || Edit.Text == "")
            {
                DialogResult msg = MessageBox.Show("Incomplete Form", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (Convert.ToDateTime(startDate.Text) > Convert.ToDateTime(endDate.Text))
                {
                    DialogResult msg = MessageBox.Show("Incorrect Date", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Assignment n = new Assignment();

                    n.Request = RequestingUnit.Text;
                    n.Address = Address.Text;
                    n.City = City.Text;
                    n.Batch = BatchNo.Text;
                    n.Assign = Desc.Text;
                    n.AssignId = Assign.Text;
                    n.Startdate = startDate.Text;
                    n.Enddate = endDate.Text;

                    String item = Edit.Text;
                    String name2, id2;
                    string[] words1 = item.Split('-');
                    String type = Type1.Text;
                    string[] words2 = type.Split('-');
                    String t;

                    name2 = words1[0];
                    id2 = words1[1];
                    t = words2[0];

                    n.Ename = name2;
                    n.Eid = id2;

                    n.Status = Status.Text;
                    n.Type = t;

                    assm.EditAssign(n);
                    main.form.assEditDisplay_Refresh();

                    DialogResult msg1 = MessageBox.Show("Assignment has been Updated in the Database", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void Type1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}